package com.lkedu.dao;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.lkedu.entity.CourseBean;
import com.lkedu.utils.DataSourceUtils;

public class CourseDao {
	
	QueryRunner runner=new QueryRunner(DataSourceUtils.getDataSource());

	public  List<CourseBean> getCourses(String keywords) {
		String sql="SELECT * FROM tb_course WHERE  cname REGEXP ? OR ctype REGEXP ?";
		Object[] params= {keywords,keywords};
		List<CourseBean> cs=null;
		try {
			cs=runner.query(sql, new BeanListHandler<CourseBean>(CourseBean.class),params);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return cs;
	}

	public  List<CourseBean> getCourses() {
		String sql="SELECT * FROM tb_course";
		List<CourseBean> cs=null;
		try {
			cs=runner.query(sql, new BeanListHandler<CourseBean>(CourseBean.class));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return cs;
	}
	

}
